package fui.FUIData
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import mx.collections.ArrayCollection;
	
	import phi.db.Database;
	import phi.db.Query;
	import phi.interfaces.IDatabase;
	import phi.interfaces.IQuery;

	public class DBClientFUI extends EventDispatcher implements IDBClient
	{
		
		// singleton
		private static var _instance:DBClientFUI;
		private var db       :IDatabase;
		private var query    :IQuery;
		private static const _Procedure:String = "clientServices.";
		private var _results:ArrayCollection;
		
		
		
		public function DBClientFUI(parameter:SingletonEnforcer):void
		{
		}
			
		/* real constructor. */
		public static function getInstance():DBClientFUI
		{
	        if(_instance == null) 
			{
	            _instance = new DBClientFUI(new SingletonEnforcer());
				_instance.db = Database.getInstance();
			    _instance.query = new Query();
				 
			    _instance.db.connect("conn1", "root", "root", "localhost", "bogo", true);
	 		    _instance.query.connect("conn1", _instance.db);
			}
			return _instance;
		}
		
		public function CallStoredProcedure(StoreProcedure:String, ... args):void
		{
			trace("---------------CallStoredProcedure-----------------");
			trace(args);
			trace(_instance.query)
			
			var Procedure:String = StoreProcedure;//_Procedure + StoreProcedure;
			//var MySQLCall:PHPCall= new PHPCall(Remoting.PHP, executeResponse, Procedure, args);
			
			_instance.query.addEventListener(Query.QUERY_END, executeResponse);
			_instance.query.execute(Procedure);
		}
		
		private function executeResponse(event:Object):void
		{
			//_instance._ResponseArray = re.result[0] as Array;
			_results = _instance.query.getRecords();
			trace("results");
			
			trace("---------------");
			trace(_results);
			
			dispatchEvent(new Event(Event.COMPLETE));
		}
		
		public function get results():ArrayCollection
		{
			return _instance._results;
		}
		
	}
}
class SingletonEnforcer {}